// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.actions;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.IDesignerUIService;
import org.dengues.designer.ui.i18n.Messages;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.internal.ui.actions.RemoveAllTerminatedAction;
import org.eclipse.debug.internal.ui.views.console.ConsoleTerminateAction;
import org.eclipse.debug.internal.ui.views.console.ProcessConsole;
import org.eclipse.jface.action.Action;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.internal.console.ConsoleView;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class StopEditorAction extends Action {

    private final LaunchEditorAction runDataBaseAction;

    public StopEditorAction(LaunchEditorAction runDataBaseAction) {
        super(Messages.getString("RunDataBaseAction.Text")); //$NON-NLS-1$
        setEnabled(false);
        setImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.TOOLBAR_STOP));
        setDisabledImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.TOOLBAR_DISABLESTOP));
        this.runDataBaseAction = runDataBaseAction;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
    public void run() {
        runDataBaseAction.setStatus(IDesignerUIService.STATUS_STOP);
        runDataBaseAction.setImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.TOOLBAR_RUN));
        runDataBaseAction.setStopThread(true);
        try {
            IViewPart findView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(
                    "org.eclipse.ui.console.ConsoleView");
            if (findView instanceof ConsoleView) {
                ConsoleView view = (ConsoleView) findView;
                IConsole console1 = view.getConsole();
                if (console1 instanceof ProcessConsole) {
                    ProcessConsole console = (ProcessConsole) console1;
                    new ConsoleTerminateAction(console).run();
                    ILaunch curlaunch = runDataBaseAction.getCurlaunch();
                    if (curlaunch != null) {
                        RemoveAllTerminatedAction.removeTerminatedLaunches(new ILaunch[] { curlaunch });
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
